/*
This file is part of vlc-remote.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

vlc-remote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import com.meego 1.0
import "common"
import "mediaRemoteFunctions.js" as MediaRemote
import "remoteStatus.js" as RemoteStatus
import "vlcRemote.js" as VlcRemote

Page {
    id: favouritesPage
    property string server: ""
    property string serverName: "Not connected"
    tools: commonTools
    //tools: menuTools

    Component.onCompleted: {
        screen.allowedOrientations = Screen.Portrait;
        //lockInPortrait = true;
        orientationLock = Screen.Portrait;

        MediaRemote.init(server);
        load();
    }

    onServerChanged: {
        if ("" == server) {
            pageStack.push(remotesPage);
        }
        else {
            MediaRemote.init(server);
            load();
        }
    }

    // JS need to be inside a JS file..
    function save()
    {
    }

    function load()
    {
        //name TEXT, type TEXT, ipAddress TEXT, port TEXT, username TEXT, password TEXT, secured BOOLEAN
        if (favouritesDirty) {
            var favs = MediaRemote.loadFavouritesByConnection(serverName);
            listModel.clear();
            for (var i = 0; i < favs.length; ++i) {
                listModel.append(favs.item(i));
            }
            listView.currentIndex = 0;
            favouritesDirty = false;
        }
    }
    onVisibleChanged:  {
        if (visible) {
            load();
        }
    }

    ScrollDecoratorStyle {
        id: scrollDecoratorStyle
        inverted: true
    }
    ScrollDecorator {
        flickableItem: listView
        platformStyle: scrollDecoratorStyle
    }
/*
    SectionScrollerStyle {
        id: sectionScrollerStyle
        fontBoldProperty: true
        fontPixelSize: 24
        textColor: "white"
        textColorHighlighted: accentColor
    }
    SectionScroller {
        listView: listView
        platformStyle: sectionScrollerStyle
    }
*/
    ButtonStyle {
        id: vlcButtonStyle
        textColor: "white"
        background: "black"
    }

    PageHeader {
        id: pageHeader
        anchors.top: favouritesPage.top
        title: qsTr("Your favourites")
        subtitle: serverName + " @ " + server + ""
        icon: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-favorite-mark-white.png"
        //onClicked: { myMenu.close(); pageStack.pop(); }
    }

    ListModel{
        id:listModel
    }

    ListView {
        id: listView
        anchors.fill: parent
        anchors.topMargin: 58
        anchors.bottomMargin: 64
        spacing: 4
        model:listModel
        //property int selection: -1
        delegate: ItemDelegate {
            //color: "black"
           // border.width: 0
           // border.color: accentColor
            title: "" + name + ""
            subTitle: "<i>" + path + "</i>"
            iconType: "favourite"
            onClicked: {
                listView.currentIndex = index;
            }
            onDoubleClicked: {
                pageHeader.notify(name + qsTr(" now playing!"), 2000);
                MediaRemote.remoteRequestTrackByPath(path);
                playlistDirty = true;
                //pageStack.push(playerPage);
            }

        }
        highlight: highlight
        highlightFollowsCurrentItem: true
        highlightMoveDuration: 2
    }

    Component {
        id: highlight
        Rectangle {
            color: "black"
            border.width: 2
            border.color: accentColor
            radius:  8
            y: (-1 < listView.currentIndex) ? listView.currentItem.y : 0
            Behavior on y {
                SpringAnimation {
                    spring: 2
                    damping: 0.2
                }
            }
        }
    }


    QueryDialog {
        id:dialog
        property string oldName: (-1 < listView.selection) ? listModel.get(listView.selection).name : ""
        titleText: qsTr("Favourite details")
        acceptButtonText: qsTr("Save details")
        rejectButtonText: qsTr("Close")

        content: Column {
            Row {
                Text {
                    width: 100
                    font.pixelSize: 20
                    color: "white"
                    text: qsTr("Name: ")
                }
                TextField {
                    id: name
                    placeholderText: "Favourite"
                    text: (-1 < listView.selection) ? listModel.get(listView.selection).name : ""
                }
            }
            Row {
                Text {
                    width: 100
                    font.pixelSize: 20
                    color: "white"
                    text: qsTr("Path: ")
                }
                TextField {
                    id: path
                    placeholderText: "Path"
                    text: (-1 < listView.selection) ? listModel.get(listView.selection).path : ""
                }
            }
         }
        onAccepted: {
            // Need to validate
            MediaRemote.deleteFavourite(oldName, serverName);
            MediaRemote.saveFavourite(name.text, serverName, path.text);
            favouritesDirty = true;
            load();
        }


    }

    QueryDialog {
        id: dialogConfirmDelete
        property alias message:  message.text
        titleText: qsTr("Really delete?")
        content:  Text {
            id: message
            width:  parent.width
            wrapMode: "WrapAtWordBoundaryOrAnywhere"
            maximumLineCount: 6
            font.pixelSize: 24
            color: "white"
            text: (-1 < listView.currentIndex) ? qsTr("Do you want to delete <b>") + listModel.get(listView.currentIndex).name + "</b>?" : ""
        }
        acceptButtonText: qsTr("Yes, delete it")
        onAccepted: {
            if (-1 < listView.currentIndex) {
                MediaRemote.deleteFavourite(listModel.get(listView.currentIndex).name, serverName);
                favouritesDirty = true;
            }
            load();
        }
        rejectButtonText: qsTr("No, keep it")
    }


    Rectangle {
        color: "black"
        width: parent.width
        height: controlsRow.height + 16
        anchors.bottom: favouritesPage.bottom
        anchors.topMargin: 0
        anchors.bottomMargin: 0

        Row {
            height: 64
            id: controlsRow
            opacity: 100
            spacing: 4
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter

            Button {
                //platformStyle: vlcButtonStyle
                id: btnPlay
                enabled: (-1 < listView.currentIndex)
                width: 86
                //text: qsTr("Connect")
                iconSource: (-1 < listView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-dimmed-white.png"
                onClicked: {
                    if (-1 < listView.selection) {
                        var path = listModel.get(listView.currentIndex).path;
                        var name = listModel.get(listView.currentIndex).name;
                        MediaRemote.remoteRequestTrackByPath(path);
                        pageHeader.notify(name + qsTr(" now playing!"), 2000);
                        playlistDirty = true;
                        //pageStack.push(playerPage);
                    }
                }
            }
            Button {
                //platformStyle: vlcButtonStyle
                id: btnQueue
                enabled: true
                width: 86
                iconSource: (-1 < listView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-content-audio-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-content-audio-dimmed-white.png"
                //text: qsTr("Add")
                onClicked: {
                    var path = listModel.get(listView.currentIndex).path;
                    var name = listModel.get(listView.currentIndex).name;
                    MediaRemote.remoteRequestQueueByPath(path);
                    pageHeader.notify(name + qsTr(" added to playlist!"), 2000);
                    playlistDirty = true;
                    // need to give feedback
                    //pageStack.push(playlistPage);
                }
            }
            Button {
                //platformStyle: vlcButtonStyle
                id: btnBrowse
                enabled: true
                width: 86
                iconSource: (-1 < listView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-directory-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-directory-dimmed-white.png"
                //text: qsTr("Add")
                onClicked: {
                    browsePage.currentDir = listModel.get(listView.currentIndex).path;
                    pageStack.push(browsePage);
                }
            }
            Button {
                //platformStyle: vlcButtonStyle
                id: btnEdit
                width: 86
                enabled: (-1 < listView.currentIndex)
                iconSource: (-1 < listView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-edit-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-edit-dimmed-white.png"
                //text: qsTr("Edit")
                onClicked: {  if (-1 < listView.currentIndex) dialog.open(); }
            }
            Button {
                //platformStyle: vlcButtonStyle
                id: btnRemove
                width: 86
                enabled: (-1 < listView.currentIndex)
                //text: qsTr("Remove")
                iconSource: (btnRemove.enabled) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-delete-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-delete-dimmed-white.png"
                onClicked: { if (-1 < listView.currentIndex) { dialogConfirmDelete.open(); } }
            }
        }
    }
}

